<template>
  <div>
    <h1>填写</h1>
    {{paramslist}}
    <p v-for="i in paramslist" :key="i.id">
      <van-field v-if="i.type=='input'" :label="i.label" type="sdate" v-model="values[i.label]"/>
      <select v-if="i.type=='select'" v-model="values[i.label]">
        <option :value="t.type" v-for="t in i.value" :key="t.id">{{ t.name }}</option>
      </select>
      <van-field v-if="i.type=='rideo'" label="性别">
        <template #input>
          <van-radio-group v-model="values[i.label]" direction="horizontal">
            <van-radio name="男">男</van-radio>
            <van-radio name="女">女</van-radio>
          </van-radio-group>
        </template>
      </van-field>
      <van-uploader v-if="i.type=='file'" :after-read="uplode">文件上传</van-uploader>

            <el-date-picker v-if="i.type=='sdate'"
                            v-model="value1"
                            type="datetimerange"
                            range-separator="To"
                            start-placeholder="开始时间"
                            format="YYYY-MM-DD"
            >
            </el-date-picker>
<!--      <el-date-picker v-if="i.type=='sdate'"-->
<!--                      v-model="value1"-->
<!--                      type="datetime"-->
<!--                      placeholder="开始时间"-->
<!--      >-->
<!--      </el-date-picker>-->
<!--      <el-date-picker v-if="i.type=='edate'"-->
<!--                      v-model="value2"-->
<!--                      type="datetime"-->
<!--                      placeholder="结束时间"-->
<!--      >-->
<!--      </el-date-picker>-->
    </p>
    {{ids}}
    <van-checkbox-group v-model="ids">
      <van-checkbox :name="i.id" v-for="i in userlist" :key="i.id">{{i.name}}</van-checkbox>

    </van-checkbox-group>
    <van-button @click="add">添加</van-button>
    <van-button @click="addd">提交</van-button>

  </div>
</template>

<script>
import myaxios from "@/http/axios";
import axios from "axios";
export default {
  name: "workf_write",
  data() {
    return {
      data1: '',
      paramslist: [],
      workff: {},
      id: this.$route.query.id,
      value1: [],
      values:{},
      pic:'',
      userlist:[],
      ids:[]
    }
  },
  methods: {
    get_params() {
      myaxios('getparams?id=' + this.id, 'get').then(res => {
        this.workff = res.list
        this.paramslist = JSON.parse(res.list.params)
        this.getusers()
      })
    },
    getlabel(type){
      let label = ''
      for (var i=0;i<this.paramslist.length;i++){
        if (this.paramslist[i]['type'] == type){
          label = this.paramslist[i]['label']
        }
      }
      return label
    },
    addd(){
      if (this.pic){
        let label = this.getlabel('file')
        this.values[label] = this.pic
      }
      if (this.value1.length>0){
        let sdate = this.getlabel('sdate')
        this.values[sdate] = this.may(this.value1[0])
        this.values[sdate] = this.may(this.value1[1])
      }
      this.values['ids'] = JSON.stringify(this.ids)
      this.values['wid'] = this.id
      myaxios('submit_task','post',this.values).then(res=>{
        alert(res.msg)
      })
    },
    may(value){
      const mydate = String(value).split(" ")
// console.log(mydate[3],mydate[1],mydate[2])
      if(mydate[1]=="Jan"){
        mydate[1]="01"
      }else if(mydate[1]=="Feb"){
        mydate[1]="02"
      } else if(mydate[1]=="Mar"){
        mydate[1]="03"
      }else if(mydate[1]=="Apr"){
        mydate[1]="04"
      }else if(mydate[1]=="Mar"){
        mydate[1]="05"
      }else if(mydate[1]=="Jun"){
        mydate[1]="06"
      }else if(mydate[1]=="Jul"){
        mydate[1]="07"
      }else if(mydate[1]=="Aug"){
        mydate[1]="08"
      }else if(mydate[1]=="Sep"){
        mydate[1]="09"
      }else if(mydate[1]=="Oct"){
        mydate[1]="10"
      }else if(mydate[1]=="Nov"){
        mydate[1]="11"
      }else if(mydate[1]=="Dec"){
        mydate[1]="12"
      }
      return mydate[3] + "-" + mydate[1] + "-" + mydate[2]
    },
    uplode(file){
      let formdate = new FormData()
      formdate.append('file',file.file)
      console.log(formdate)
      axios({url:'http://127.0.0.1:5000/imgfile',method:'post',data:formdate}).then(res=>{
        this.pic = res.data.list
      })
    },
    getusers(){
      myaxios('getusers','get').then(res=>{
        this.userlist = res.list
      })
    },
  },
  mounted() {
    this.get_params()
  }
}
</script>

<style scoped>

</style>